﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Registracija : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        mv_registracija.ActiveViewIndex = 0;
    }
    protected void btn_registracija_Click(object sender, EventArgs e)
    {
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString =ConfigurationManager.ConnectionStrings["Konekcija"].ConnectionString;

        string insertSql, checkNoMem;
        checkNoMem = "SELECT * FROM Photographers";
        insertSql = "INSERT INTO Photographers (first_name,last_name,e_mail,username,password)";
        insertSql += "VALUES(";
        insertSql += "@first_name,@last_name,@e_mail,@username,@password)";

        SqlCommand cmd = new SqlCommand(insertSql, myConnection);
        SqlCommand komanda = new SqlCommand(checkNoMem, myConnection);
        SqlDataReader citac;
        int broj_clenovi = 0;

        cmd.Parameters.AddWithValue("@first_name", TextBox1.Text);
        cmd.Parameters.AddWithValue("@last_name", TextBox2.Text);
        cmd.Parameters.AddWithValue("@e_mail", TextBox3.Text);
        cmd.Parameters.AddWithValue("@username", TextBox4.Text);
        cmd.Parameters.AddWithValue("@password", TextBox5.Text);

        int added = 0;
        try
        {
            myConnection.Open();
            added = cmd.ExecuteNonQuery();
            lbl_result1.Text = added.ToString() + " records inserted.";
            lbl_user.Text = TextBox1.Text + " " + TextBox2.Text;
            Session["username"] = TextBox4.Text;

            citac = komanda.ExecuteReader();
            while (citac.Read())
            {
                broj_clenovi++;
            }
            lbl_member.Text = broj_clenovi.ToString() + " ";

            mv_registracija.ActiveViewIndex = 1;
        }
        catch (Exception err)
        {
            lbl_result1.Text = "Username already exists! ";
            //lblInfo.Text += err.Message;
        }
        finally
        {
            myConnection.Close();
        }
    }
    protected void mv_registracija_ActiveViewChanged(object sender, EventArgs e)
    {

    }
    protected void lnk_Enter_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
        TextBox6.Text = "";
        Response.Redirect("GlavnaStrana.aspx");       
    }
}
